package com.qumitech.visualnovel.service.position.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.position.PositionConfigDTO;
import com.qumitech.visualnovel.pubdef.pubenum.OperationPositionTypeEnum;
import com.qumitech.visualnovel.service.position.PositionConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2019/5/28 16:22
 * @Description:
 */
@Service
public class PositionConfigServiceImpl implements PositionConfigService {
    public final String POSITION = "/position";
    public final String POSITION_ADMIN = "/admin/position";


    @Override
    public void addPositionCfg(PositionConfigDTO positionConfigDTO) {
        String url = UrlConstant.SERVER_URL + POSITION;
        String req = JSONObject.toJSONString(positionConfigDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void updatePositionCfg(PositionConfigDTO positionConfigDTO) {
        String url = UrlConstant.SERVER_URL + POSITION;
        String req = JSONObject.toJSONString(positionConfigDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getPositionCfg(OperationPositionTypeEnum typeEnum, Integer offset, Integer limit, String appVersion, String appChannel) {
        String url = UrlConstant.SERVER_URL + POSITION_ADMIN;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("view_type", typeEnum.getPositionType().toString());
        paramMap.put("offset", offset.toString());
        paramMap.put("limit", limit.toString());
        if(appVersion != null){
            paramMap.put("app_version", appVersion);
        }
        if(appChannel != null){
            paramMap.put("app_channel", appChannel);
        }
        String body = HttpInvokeUtil.httpGet(url, paramMap,null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        if (rspMap.get("config") == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rspMap;
    }
}
